var QS_PermissionController = function(){
    this.moduleConfigs = {};
    this.permission = GOLBAL_DATA.ModulePermission||[];
	this.permission=this.permission.concat([24,27,21,22,23,1.1,1.2,1.3,1.4,3.1211,3.1212,3.1213,3.1214,3.1215,3.1311,3.1312,3.1313,
    3.1314,3.1411,3.1412,3.1511,3.1512,3.1513,3.1514,3.1515,3.1611,3.1612,3.1613,3.1614,3.1615,3.1616,3.2111,3.2112,3.2113,3.2114,3.2115,3.2116,3.2511,3.2512,/*3.2513,*/3.2514,3.2515,3.2516,3.2517,3.2611,3.2612,3.2613,3.2614,3.29,3.31,3.32,3.33,3.34,3.3411,3.3412,3.35,3.36,3.3611,3.3612,3.3613,3.3614,3.3615,3.37,3.38,3.39,3.3911,3.3912,3.3913,3.3914,3.41,3.4111,3.4112,/*3.4113,*/3.4114,3.4115,3.4116,3.42,3.43,3.4311,3.4312,3.4313,3.4314,3.44,3.45,3.46,3.47,3.48,3.4811,3.4812,3.4813]);
	
	//if it is a direct user.
	if(GOLBAL_DATA.SSOFrom&&GOLBAL_DATA.SSOFrom.toLowerCase() == 'direct'){
		this.permission=this.permission.concat([3.2513,3.4113]);
	}
	
    this._pCache = {};
    this.init();
};
QS_PermissionController.prototype = {
    init:function(){
        this._createConfig();
    },
    //checkPermission(code, el) el will be hide if no permission
    //checkPermission(code, callback)
    checkPermission:function(code){
        var hasPermission = false;
		// check from cache
		if (this._pCache[code]) {
			hasPermission = this._pCache[code];
		}
		else {
			if ($.inArray(code, this.permission) > -1) {
				hasPermission = true;
			}
			this._pCache[code] = hasPermission;
		}
		
		if (typeof arguments[1] == 'Function') {
			arguments[1](hasPermission);
		}
		else 
			if (arguments[1] && !hasPermission) {
				$(arguments[1]).hide();
			}
        return hasPermission;
    },
    getConfig:function(ComponentCode, sourceCfg){
        if (sourceCfg) {
            $.extend(true, sourceCfg, ComponentCode);
            return sourceCfg;
        }
        else {
            return this.moduleConfigs[ComponentCode] || null;
        }
    },
    _createConfig:function(){
        this.moduleConfigs = {
            0: {
                mstartColumn: this.checkPermission(0.01),
                showAlert: this.checkPermission(4),
                isDotComUser: this.checkPermission(0.03),
                isOfficeUser: GOLBAL_DATA.SSOFrom&&GOLBAL_DATA.SSOFrom.toLowerCase() == 'office' ? true : this.checkPermission(0.04),
                isDirectUser: GOLBAL_DATA.SSOFrom&&GOLBAL_DATA.SSOFrom.toLowerCase() == 'direct' ? true : this.checkPermission(0.05),
				isLPLUser: this.checkPermission(0.06),
                SNP_GICS: this.checkPermission(0.02) //S&P GICS
            },
            1: {
				indicator:{SMA:true,
					 EMA:true,
             		 SMAV:true,
					 BBands:true,
                     PSAR:true,
                     MACD:true,
                     RSI:true,
                     ROC:true,
                     WillR:true,
                     SStochastic:true,
                     FStochastic:true,
					 PChannel:true,
					 MAE:true,
					 Momentum:true,
					 ULT:true,
					 DMI:true,
					 MFI:true,
					 OBV:true,
					 VAcc:true,
					 UDRatio:true,
					 VBP:true,
					 Volume:true,
					 VPlus:true},
				valuations:{
					PE:true,
					PS:true,
					PB:true,
					PC:true,
					RDividend:true,
					DYield:true,
					REPS:true,
					SInterest:true,
					FairValue: this.checkPermission(1.01)
				},
				drawing:{
					trendline:true,
					rectangle:true,
					ellipse:true,
					note:true
				},
				events:{
					dividend:true,
					splits:true,
					earnings:true
				}
            }, //ChartWindow,
            2: {
				dcSourceList:{
					"M*A" :this.checkPermission(2.2),
                    "DJ":this.checkPermission(2.3),
                    "FT":this.checkPermission(2.7),
                    "TPX":this.checkPermission(2.12),
                    "MW":this.checkPermission(2.11)
				},
				ntSourceList:{
					//Stock Market Update
					"3080":this.checkPermission(2.16)?false:this.checkPermission(2.15)?false:true,
					"3087":this.checkPermission(2.16)?false:this.checkPermission(2.15),
					"3088":this.checkPermission(2.16),
					//BOND_MARKET_UPDATE
					"3089":this.checkPermission(2.16)?false:this.checkPermission(2.15)?false:true,
                    "3090":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3091":this.checkPermission(2.16),
					//CALENDAR
					"3093":this.checkPermission(2.16)?false:this.checkPermission(2.15)?false:true,
                    "3094":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3095":this.checkPermission(2.16),
					//Market Internals
                    "3096":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3097":this.checkPermission(2.16),
					//Ahead of The Curve
					"3099":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3119":this.checkPermission(2.16),
					//Economic Date Previews
                    "3100":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3120":this.checkPermission(2.16),
					//Economic Date Reviews
                    "3101":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3121":this.checkPermission(2.16),
					//Economic Insight
                    "3102":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3122":this.checkPermission(2.16),
					//Emerging 50
					"3103":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3123":this.checkPermission(2.16),
					//Emerging Growth Stocks
                    "3104":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3124":this.checkPermission(2.16),
					//ETF Daily Notes
                    "3125":this.checkPermission(2.16),
					//ETF Portfolio
                    "3126":this.checkPermission(2.16),
					//Industry Insight
                    "3105":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3127":this.checkPermission(2.16),
					//Liquid Momentum
                    "3106":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3128":this.checkPermission(2.16),
					//Market Outlook
                    "3107":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3129":this.checkPermission(2.16),
					//Page One
                    "3108":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3130":this.checkPermission(2.16),
					//Previews
                    "3109":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3131":this.checkPermission(2.16),
					//Special Reports
                    "3110":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3132":this.checkPermission(2.16),
					//Stock Ideas
                    "3111":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3133":this.checkPermission(2.16),
					//Strategy Meeting Key Points
					"3112":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3134":this.checkPermission(2.16),
					//Swing Trader
                    "3113":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3135":this.checkPermission(2.16),
					//Taking Stock
                    "3114":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3136":this.checkPermission(2.16),
					//TA Page Scans
                    "3137":this.checkPermission(2.16),
					//TA Page Set Ups
                    "3138":this.checkPermission(2.16),
					//The Big Picture
					"3115":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3139":this.checkPermission(2.16),
					//The Next Big Thing
                    "3116":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3140":this.checkPermission(2.16),
                    //The Technical Take
                    "3117":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3141":this.checkPermission(2.16),
					//Week In Perspective
                    "3118":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3142":this.checkPermission(2.16),
					//Economic Releases
					"3143":this.checkPermission(2.16)?false:this.checkPermission(2.15)?false:true,
                    "3144":this.checkPermission(2.16)?false:this.checkPermission(2.15),
                    "3145":this.checkPermission(2.16),
					//BREFINGINPLAY
					"3044":this.checkPermission(2.16)?false:this.checkPermission(2.15),
					//BREFINGTRADER
                    "3006":this.checkPermission(2.16),
			        					
					"4023":this.checkPermission(2.142),//MKTNEWS_ALERTS__
                    "4024":this.checkPermission(2.142),//MKTNEWS_BULLETS_
                    "4025":this.checkPermission(2.142),//MKTNEWS_MAINWIRE
                    "4026":this.checkPermission(2.142),//MKTNEWS_BULLETXI
                    "4027":this.checkPermission(2.142),//MKTNEWS_SELECT__
                    "4016":this.checkPermission(2.143),//JAGMEDIAINTRADAY
                    "4017":this.checkPermission(2.143),//JAGMEDIAMORNING_
                    "4018":this.checkPermission(2.143),//JAGMEDIARUMORS__
                    "4019":this.checkPermission(2.143),//JAGMEDIASUMMARYI
                    "4020":this.checkPermission(2.143),//JAGMEDIASUMMARYM
                    "4002":this.checkPermission(2.146),//AP______FINANCE_
                    "4043":this.checkPermission(2.147),//MIDNIGHTUSEQUITY
                    "4044":this.checkPermission(2.148),//MIDNIGHT__CN
                    "4004":this.checkPermission(2.144),//BIZFINNWNEWS____
                    "4012":this.checkPermission(2.141),//FLYWALL_USEQUITY
                    "4013":this.checkPermission(2.145),//FXSTREETNEWSCHRT
                    "2001":this.checkPermission(2.101),//Morningstar Stock Analyst Notes
                    "2002":this.checkPermission(2.101),//Morningstar Stock Analysis
                    "2003":this.checkPermission(2.101),//Morningstar Fund Analysis
                    "2004":this.checkPermission(2.101),//Morningstar ETF Analysis
                    "6020":false,
					"6019":false,
					"7001":false,
                    "2005":false,//Morningstar Videos
                    "2008":false,//Footnoted.org Premium
                    "5004":false,//jump123
                    
					//tenfore DJ source
                    "8001":this.checkPermission(2.171),//Dow Jones NL (Dutch) News
                    "8002":this.checkPermission(2.172),//Dow Jones News General
                    "8003":this.checkPermission(2.173),//Dow Jones European Corporate Reports
                    "8004":this.checkPermission(2.174),//Dow Jones Market Beat
                    "8005":this.checkPermission(2.175),//Dow Jones Economic Report
                    "8006":this.checkPermission(2.176),//DJ Equity Spotlight
                    "8008":this.checkPermission(2.178),//Dow Jones Bourse Pro (FR)
                    "8009":this.checkPermission(2.179),//Dow Jones Espanol
                    "8010":this.checkPermission(2.1711),//DJ Asian Equity Report
                    "8011":this.checkPermission(2.1712),//DJ Bankers Report
                    "8012":this.checkPermission(2.1713)//DJ Money Spotlight
				},
				b_sourceList:{
					"BrokerResearch":this.checkPermission(2.15),
					"Commentary":this.checkPermission(2.15),
					"Earnings":this.checkPermission(2.15),
					"Econ":this.checkPermission(2.15),
					"News":this.checkPermission(2.15),
					"Options":this.checkPermission(2.15),
					"Profiles":this.checkPermission(2.15),
					"Rumors":this.checkPermission(2.15),
					"Summaries":this.checkPermission(2.15),
					"TechnicalAnalysis":this.checkPermission(2.15),
					"TopEvents":this.checkPermission(2.15),
					"GeneralTradingCalls":this.checkPermission(2.15),
					"IntraDayTrades":this.checkPermission(2.15),
					"ScalpTrades":this.checkPermission(2.15),
					"SwingTrades":this.checkPermission(2.15)
				},
				showAnalystReport:this.checkPermission(2.101)
			}, //NewsWindow,
            3: {
                tabList:{
                    "Profile":this.checkPermission(3.11),
                    "Analysis":this.checkPermission(3.12),
                    "Financials":this.checkPermission(3.13),       
                    "Valuation":this.checkPermission(3.14),
                    "Shareholders":this.checkPermission(3.15),
                    "Insiders":this.checkPermission(3.16),
                    "Filings":this.checkPermission(3.17),
                    "Bonds":this.checkPermission(3.18),
                    "Keyratio":this.checkPermission(3.19),
                    "Performance":this.checkPermission(3.21),
                    "Industrypeers":this.checkPermission(3.22),
                    "RatingRisk":this.checkPermission(3.23),
                    "Taxanalysis":this.checkPermission(3.24)
                }
            }, //QtWindow,
            4: null, //alert,
            5: null, //tickertape,
            6: null, //L2MakerDepthWindow,
            7: null, //TimeSalesWindow,
            8: null, //OptionsWindow,
            9: null, //SRWindow,
            10: null, //QuotesWindow,
            11: null, //ForexWindow,
            12: null, //CalendarWindow,
            13: null, //CommodityWindow
            14:null//market
        };
    }
};
var QSPC = new QS_PermissionController();


/**
 * @author {Yuntao Zhou}
 * depends on permission.js
 */
var QS_SecurityModuleController=function(){
	this.moduleConfigs={};
	this._pCache={};
	this.init();
};

QS_SecurityModuleController.prototype={
	init:function(){
		this._createConfig();
	},
	_createConfig:function(){
		this.moduleConfigs={
			'ST':[0,1,1.1,1.2,1.3,1.4,2,3,3.11,3.12,3.13,3.14,3.15,3.16,3.17,3.18,3.19,3.21,3.22,3.1211,3.1212,3.1213,3.1311,3.1312,3.1313,
                3.1314,3.1411,3.1412,3.1511,3.1512,3.1513,3.1514,3.1515,3.1611,3.1612,3.1613,3.1614,3.1615,3.1616,3.2111,3.2112,3.2113,3.37,3.44,3.48,3.4811,3.4812,3.4813,5,6,7,8,9,10],//[] stock			
			'FE':[0,1,1.1,1.2,1.3,1.4,2,3,3.12,3.1214,3.1215,3.15,3.1511,3.1512,3.1513,3.1514,3.1515,3.23,3.24,3.25,3.26,3.27,3.2511,3.2512,3.2513,3.2514,3.2515,3.2516,3.28,3.45,5,6,7,8,9,10],//[] ETF
			'FC':[0,1,1.1,1.2,1.3,1.4,2,3,3.15,3.1511,3.1512,3.1513,3.1514,3.1515,3.24,3.27,3.2511,3.2512,3.2513,3.31,3.32,3.34,3.3411,3.3412,3.35,3.36,3.3611,3.3612,3.3613,3.3614,3.3615,3.46,5,6,7,8,9,10],//[] CE
			'FO':[0,1,2,3,3.21,9,3.2114,3.2115,3.23,3.24,3.25,3.27,3.33,3.2511,3.2512,3.2513,3.2514,3.2515,3.2516,3.47,5,10],//[] MF
            'XI':[0,1,1.1,1.2,1.3,1.4,2,5,6,7,8,9,10,17],
			'FX':[0,1,1.1,1.2,1.3,5,10], //add forex,2011.9.7,alex
			'FM':[3,3.38,3.39,3.3911,3.3912,3.3913,3.3914,3.41,3.4111,3.4112,3.4113,3.4114,3.4115,3.4116,3.42,3.43,3.4311,3.4312,3.4313,3.4314,5],//market money fund added for quiktake
			'FU':[0,5,17], //add by songlin for futures permission of watchlist, market commodity futures
			'PR':[3,3.11,3.13,3.15,3.16,3.17,3.19,3.21,3.22,3.44,3.1211,3.1212,3.1213,3.1311,3.1312,3.1313,
                3.1314,3.1511,3.1512,3.1513,3.1514,3.1515,3.1611,3.1612,3.1613,3.1614,3.1615,3.1616,3.2111,3.2112,3.2113,3.2116,3.48,3.4811,3.4812,3.4813],//add by songlin for quictake Preferred Stock
			"Bond":[0,10,7],
			"Spread":[0,10,7],
			"Options":[0,1,6,7,9,10]
		};
	},
	checkSecurityModule:function(sType,mCode){ 
		var hasPermission = false;
		if(typeof sType!='undefined'&&typeof mCode!='undefined'){
			var key=sType+"_"+mCode;
			if (QSPC.checkPermission(mCode)) {
				if(this._pCache[key]){
					hasPermission=this._pCache[key];
				}else{
					if(this.moduleConfigs[sType]&&$.inArray(mCode,this.moduleConfigs[sType])>-1){
						hasPermission = true;
						this._pCache[key] = hasPermission;
					}
				}
			}
		}
		return hasPermission;
	},
    checkSecurityType:function(sType,mCode){ // check the sec permission in component.
        var hasPermission = false;
        if(typeof sType!='undefined'&&typeof mCode!='undefined'){
            if(this.moduleConfigs[sType]&&$.inArray(mCode,this.moduleConfigs[sType])>-1){
                hasPermission = true;
            }
        }
        return hasPermission;
    },
	getModuleConfig:function(sType,codes){
		var mCodes=this.moduleConfigs[sType]||null;
		if(mCodes==null){
			return null;
		}else{
			var res=[];
			if(typeof codes=='undefined'||codes==null){
				codes=[];
			}
			if(codes.length==0){
				for (var i = 0, l = mCodes.length; i < l; i++) {
					if (QSPC.checkPermission(mCodes[i])) {
						res.push(mCodes[i]);
					}
				}
			}else{
				for(var i=0,l=codes.length;i<l;i++){
					if (QSPC.checkPermission(codes[i])&&$.inArray(codes[i],mCodes)>-1) {
						res.push(codes[i]);
					}
				}
			}
			return res.length==0?null:res;
		}	
	},
	getSTypeConfig:function(mCode){
		if(QSPC.checkPermission(mCode)){
			var res=[];
			for(var s in this.moduleConfigs){
				if(this.moduleConfigs[s]&&$.inArray(mCode,this.moduleConfigs[s])>-1){
					res.push(s);
				}
			}
			return res.length==0?null:res;
		}else{
			return null;
		}
	}
};
var QS_SMC=new QS_SecurityModuleController();
